<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2007/01/05 20:57:52 $)
// $RCSfile: tickets.php,v $ : $Revision: 1.41 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

define("POST_STAFF", 1);
define("POST_CLIENT", 2);
define("POST_THIRDPARTY", 3);
define("POST_RECIPIENT", 4);
define("POST_USER", 2);

define("TICKET_STAFF", 1);
define("TICKET_CLIENT", 2);

define("TICKET_WEB", 1);
define("TICKET_EMAIL", 2);
define("TICKET_API", 3);

define("ACTION_TICKETNOTE", 1);
define("ACTION_USERNOTE", 2);
define("ACTION_FLAG", 3);
define("ACTION_MOVE", 4);
define("ACTION_MERGE", 5);
define("ACTION_EDIT", 6);
define("ACTION_DELETEPOST", 7);
define("ACTION_STATUS", 8);
define("ACTION_PRIORITY", 9);
define("ACTION_ASSIGN", 10);
define("ACTION_NEWTICKET", 11);
define("ACTION_NEWREPLY", 12);
define("ACTION_ESCALATION", 13);
define("ACTION_DUE", 14);
define("ACTION_SLA", 15);
define("ACTION_XMLEXPORT", 16);
define("ACTION_PDFEXPORT", 17);
define("ACTION_DELETENOTE", 18);
define("ACTION_PRINT", 19);
define("ACTION_DELETETICKETPOST", 20);
define("ACTION_CLEARDRAFT", 21);
define("ACTION_FORWARD", 22);
define("ACTION_RECIPIENTDEL", 23);
define("ACTION_DELETETIMETRACK", 24);
define("ACTION_TIMETRACK", 25);
define("ACTION_PARSERRULE", 26);
define("ACTION_MERGE", 27);
define("ACTION_DELETETICKET", 28);

define("LOG_STAFF", 1);
define("LOG_PARSER", 2);
define("LOG_CLIENT", 3);
define("LOG_ESCALATION", 4);
define("LOG_SYSTEM", 5);

define("ALERT_NEWTICKET", 1);
define("ALERT_NEWREPLY", 2);
define("ALERT_MOVE", 3);
define("ALERT_FLAG", 4);
define("ALERT_NOTE", 5);
define("ALERT_CLIENTNOTE", 6);
define("ALERT_STATUS", 7);
define("ALERT_EDIT", 8);
define("ALERT_PRIORITY", 9);
define("ALERT_ASSIGN", 10);
define("ALERT_MERGE", 11);
define("ALERT_DELETEPOST", 12);
define("ALERT_DELETETICKET", 13);
define("ALERT_NEWSTAFFREPLY", 14);

define("ALERT_EMAIL", 1);
define("ALERT_SMS", 2);
define("ALERT_PUBLIC", 1);
define("ALERT_PRIVATE", 0);

define("SEARCH_POSTINDEX", 1);
define("SEARCH_FULLTEXT", 2);

define("STATUS_OPEN", 1);
define("STATUS_ONHOLD", 2);
define("STATUS_CLOSED", 3);

define("CATEGORY_PUBLIC", 1);
define("CATEGORY_PRIVATE", 2);

define("FLAG_PURPLE", 1);
define("FLAG_ORANGE", 2);
define("FLAG_GREEN", 3);
define("FLAG_YELLOW", 4);
define("FLAG_RED", 5);
define("FLAG_BLUE", 6);

define("VIEW_PUBLIC", 1);
define("VIEW_PRIVATE", 0);
define("VIEWFIELD_LINK", 1);
define("VIEWFIELD_CUSTOM", 2);

define("VIEW_UNASSIGNED", 1);
define("VIEW_ASSIGNED", 2);
define("VIEW_ALLTICKETS", 3);
define("VIEW_ASC", "1");
define("VIEW_DESC", "2");

define("CUSTOMVIEW_COUNT", 1);
define("CUSTOMVIEW_FETCH", 2);

define("NOTE_TICKET", 1);
define("NOTE_USER", 2);

define("RECIPIENT_THIRDPARTY", 1);
define("RECIPIENT_CCUSER", 2);
define("RECIPIENT_BCCUSER", 3);

define("ATTACHMENT_DB", 1);
define("ATTACHMENT_FILE", 2);
define("ATTACHMENT_DL", 3);

define("SEARCH_TICKETID", 1);
define("SEARCH_TICKETIDLIST", 2);
define("SEARCH_TICKETMASK", 3);
define("SEARCH_DELIM", " ");

define("OP_EQUAL", 1);
define("OP_NOTEQUAL", 2);
define("OP_REGEXP", 3);
define("OP_CONTAINS", 4);
define("OP_NOTCONTAINS", 5);
define("OP_GREATER", 6);
define("OP_LESS", 7);

/**
* Tickets Module
*/
class tickets {

	/**
	* Constructor to register any events, sections
	*/
	function tickets() {
		global $_SWIFT, $events, $datastore;

		$events->registerEventModuleSWIFT(MODULE_TICKETS, $this);

		// Admin CP: Tickets
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "managestatus");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "insertstatus");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "editstatus");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "managepriorities");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "insertpriority");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "purgeattachments");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "attachsearch");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "deleteattach");

		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "editpriority");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "attachmenttypes");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "editattachmenttype");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "maintenance");

		// Admin CP: SLA
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "manageplans");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "manageschedules");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "insertplan");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "editplan");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "insertschedule");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "editschedule");

		// Admin CP: Escalations
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "manageescalationrules");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "insertescalationrule");
		$events->registerEvent(EVENT_ADMIN, MODULE_TICKETS, "editescalationrule");

		// Staff CP
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "managepredcat");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "insertpredcat");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "editpredcat");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "managepredreplies");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "insertpredreply");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "editpredreply");

		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "managealerts");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "insertalert");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "editalert");

		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "manage");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "viewticket");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "viewhistory");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "search");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "advancedsearch");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "newticketview");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "editticketview");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "delticketview");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "ticketactions");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "ajax");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "newticket");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "editticketpost");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "reports");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "managefilters");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "insertfilter");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "editfilter");
		$events->registerEvent(EVENT_STAFF, MODULE_TICKETS, "runfilter");

		// RSS
		$events->registerEvent(EVENT_RSS, MODULE_TICKETS, "rss");

		// Client CP
		$events->registerEvent(EVENT_CLIENT, MODULE_TICKETS, "submit");
		$events->registerEvent(EVENT_CLIENT, MODULE_TICKETS, "viewlist");
		$events->registerEvent(EVENT_CLIENT, MODULE_TICKETS, "postreply");
		$events->registerEvent(EVENT_CLIENT, MODULE_TICKETS, "viewticket");
		$events->registerEvent(EVENT_CLIENT, MODULE_TICKETS, "getattachment");
		$events->registerEvent(EVENT_CLIENT, MODULE_TICKETS, "modernbillv5");

		// PDA
		$events->registerEvent(EVENT_PDA, MODULE_TICKETS, "manage");
		$events->registerEvent(EVENT_PDA, MODULE_TICKETS, "viewticket");
		$events->registerEvent(EVENT_PDA, MODULE_TICKETS, "ticketactions");

		// Cron
		$events->registerEvent(EVENT_CRON, MODULE_TICKETS, "cron");
		$events->registerEvent(EVENT_CRON, MODULE_TICKETS, "cronfollowup");

		// InstaAlert
		$events->registerEvent(EVENT_INSTAALERT, MODULE_TICKETS, "instaalert");

		// WAP
		$events->registerEvent(EVENT_WAP, MODULE_TICKETS, "list");
		$events->registerEvent(EVENT_WAP, MODULE_TICKETS, "viewticket");
		$events->registerEvent(EVENT_WAP, MODULE_TICKETS, "reply");

		// Mobile
		$events->registerEvent(EVENT_MOBILE, MODULE_TICKETS, "syncfetch");
		$events->registerEvent(EVENT_MOBILE, MODULE_TICKETS, "mobilepush");
		$events->registerEvent(EVENT_MOBILE, MODULE_TICKETS, "mobilenewticket");

		// ======= CALLBACK =======
		$events->registerEvent(EVENT_CALLBACK, MODULE_TICKETS, "mobileLogin");
		$events->registerEvent(EVENT_CALLBACK, MODULE_TICKETS, "mobileLogout");
		$events->registerEvent(EVENT_CALLBACK, MODULE_TICKETS, "clirebuildindex");

		$eventaction = $events->eventdata["eventaction"];
		$datastore->cache("prioritycache");
		$datastore->cache("statuscache");
		$datastore->cache("staffcache");
		$datastore->cache("departmentcache");
		$datastore->cache("slaplancache");
		$datastore->cache("slaschedulecache");
		$datastore->cache("escalationrulecache");
		$datastore->cache("attachmenttypescache");
		$datastore->cache("ticketcountcache");
		$datastore->cache("ticketgridcache");
		$datastore->cache("ticketviewcache");
		$datastore->cache("queuecache");
		$datastore->cache("instaalertcache");
		$datastore->cache("ticketlabelcache");
		$datastore->cache("labelcountcache");
		$datastore->cache("ticketfiltercache");
	}

	/**
	* Destructor, called to unregister any events etc
	*/
	function _tickets() {
	}

	/*
	* Call back function
	*/
	function _eventcallback($eventtype, $eventaction)
	{
		global $_SWIFT, $interface, $template, $departments, $staffauth, $module, $grid, $xml, $staff, $settings, $dbCore, $events, $session, $loginshare, $cookie, $errormessage, $infomessage, $permissions, $mimelist;

		if ($eventtype == EVENT_STAFF && ($eventaction == "manage")) {
			require_once ("./modules/tickets/staff_manage.php");
			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "viewticket" || $eventaction == "viewhistory")) {
			require_once ("./modules/tickets/staff_viewticket.php");
			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "ticketactions")) {
			require_once ("./modules/tickets/staff_ticketactions.php");
			return true;
		} else if ($eventtype == EVENT_RSS && ($eventaction == "rss")) {
			require_once ("./modules/tickets/rss_view.php");
			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "newticketview" || $eventaction == "editticketview" || $eventaction == "delticketview")) {
			require_once ("./modules/tickets/staff_views.php");
			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "ajax")) {
			require_once ("./modules/tickets/staff_ajax.php");
			return true;
		} else if ($eventtype == EVENT_CLIENT && ($eventaction == "submit")) {
			require_once ("./modules/tickets/client_submit.php");
			return true;
		} else if ($eventtype == EVENT_CLIENT && ($eventaction == "modernbillv5")) {
			require_once ("./modules/tickets/client_modernbillv5.php");
			return true;
		} else if ($eventtype == EVENT_CLIENT && ($eventaction == "viewlist" || $eventaction == "viewticket" || $eventaction == "postreply" || $eventaction == "getattachment")) {
			require_once ("./modules/tickets/client_tickets.php");
			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "advancedsearch" || $eventaction == "search")) {
			require_once ("./modules/tickets/staff_search.php");
			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "newticket")) {
			require_once ("./modules/tickets/staff_newticket.php");
			return true;
		} else if ($eventtype == EVENT_MOBILE && ($eventaction == "syncfetch" || $eventaction == "mobilepush" || $eventaction == "mobilenewticket")) {
			require_once ("./modules/tickets/mobile_sync.php");
			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "managefilters" || $eventaction == "insertfilter" || $eventaction == "editfilter" || $eventaction == "filterimportexport" || $eventaction == "runfilter")) {
			require_once ("./modules/tickets/staff_filters.php");
			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "managealerts" || $eventaction == "insertalert" || $eventaction == "editalert")) {
			require_once ("./modules/tickets/staff_alertrules.php");
			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "managepredcat" || $eventaction == "insertpredcat" || $eventaction == "editpredcat" || $eventaction == "managepredreplies" || $eventaction == "insertpredreply" || $eventaction == "editpredreply")) {
			require_once ("./modules/tickets/staff_predefined.php");
			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "reports")) {
			require_once ("./modules/tickets/staff_reports.php");
			return true;
		} else if ($eventtype == EVENT_PDA && ($eventaction == "manage" || $eventaction == "viewticket" || $eventaction == "ticketactions")) {
			require_once ("./modules/tickets/pda_manage.php");
			return true;
		} else if ($eventtype == EVENT_CRON && ($eventaction == "cron")) {
			require_once ("./modules/tickets/cron_tickets.php");
			return true;
		} else if ($eventtype == EVENT_CRON && ($eventaction == "cronfollowup")) {
			require_once ("./modules/tickets/cron_followup.php");
			return true;
		} else if ($eventtype == EVENT_INSTAALERT && ($eventaction == "instaalert")) {
			require_once ("./modules/tickets/instaalert_dispatch.php");
			return true;
		} else if ($eventtype == EVENT_CALLBACK && ($eventaction == "mobileLogin" || $eventaction == "mobileLogout")) {
			require_once ("./modules/tickets/mobile_initexit.php");
			return true;
		} else if ($eventtype == EVENT_WAP && ($eventaction == "list" || $eventaction == "viewticket" || $eventaction == "reply")) {
			require_once ("./modules/tickets/wap_manage.php");
			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "editticketpost")) {
			require_once ("./modules/tickets/staff_editpost.php");
			return true;
		} else if ($eventtype == EVENT_ADMIN && ($eventaction == "managestatus" || $eventaction == "insertstatus" || $eventaction == "managepriorities" || $eventaction == "insertpriority" || $eventaction == "attachmenttypes" || $eventaction == "editattachmenttype" || $eventaction == "editstatus" || $eventaction == "editpriority")) {
			require_once ("./modules/tickets/admin_tickets.php");
			return true;
		}else if ($eventtype == EVENT_ADMIN && $eventaction == "purgeattachments" || $eventaction == "attachsearch" || $eventaction == "deleteattach") {
			require_once ("./modules/tickets/purge_attachments.php");
			return true;
		} else if ($eventtype == EVENT_ADMIN && ($eventaction == "manageplans" || $eventaction == "manageschedules" || $eventaction == "insertplan" || $eventaction == "insertschedule" || $eventaction == "editplan" || $eventaction == "editschedule")) {
			require_once ("./modules/tickets/admin_sla.php");
			return true;
		} else if ($eventtype == EVENT_ADMIN && ($eventaction == "manageescalationrules" || $eventaction == "insertescalationrule" || $eventaction == "editescalationrule")) {
			require_once ("./modules/tickets/admin_escalations.php");
			return true;
		} else if ($eventtype == EVENT_ADMIN && ($eventaction == "maintenance")) {
			require_once ("./modules/tickets/admin_maintenance.php");
			return true;
		} else if ($eventtype == EVENT_CALLBACK && ($eventaction == "clirebuildindex")) {
			require_once ("./modules/tickets/cli_rebuildindex.php");
			return true;
		}

		return false;
	}
}
?>